//
//  ShaiDanCell.m
//  danpin
//
//  Created by chuangjia on 6/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "ShaiDanCell.h"
#import "viewconfig.h"
#import "UIPlaceholderTextView.h"
@interface ShaiDanCell()<UITextViewDelegate>
{
    
}

@property(nonatomic,strong)UIImageView *imagesView;
@property(nonatomic,strong)UIPlaceholderTextView *mTextView;
@end

@implementation ShaiDanCell

-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
         UITapGestureRecognizer* singleOneView= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapForSunDetail)];
        UIImage *imag =  [UIImage imageNamed:@"cjsd_image"];
        _imagesView =[[UIImageView alloc] initWithFrame:CGRectMake((DEVICE_WIDTH-widthAuto(280))/2, 0, imag.size.width,imag.size.height)];
        _imagesView.backgroundColor =UIColorFromRGB(0xf4f4f4);
        _imagesView.contentMode = UIViewContentModeScaleToFill;
        _imagesView.image =imag;
        _imagesView.userInteractionEnabled=YES;
        [_imagesView addGestureRecognizer:singleOneView];
        [self.contentView addSubview:_imagesView];
        singleOneView=nil;
        
        _mTextView =[[UIPlaceholderTextView alloc]initWithFrame:CGRectZero];
        _mTextView.backgroundColor =[UIColor clearColor];
        _mTextView.delegate = self;
        _mTextView.placeholderColor =[UIColor darkGrayColor];
        _mTextView.textColor = [UIColor darkGrayColor];
        _mTextView.placeholder = @"文字描述...";
     
        
        _mTextView.font =FONT(15);
        _mTextView.returnKeyType = UIReturnKeyDone;
        [self.contentView addSubview:_mTextView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChangedWillHide:) name:UIKeyboardWillHideNotification object:nil];
   }
    return self;
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.actionBlock =nil;
    self.data=nil;
    self.imagesView = nil;
    self.key =nil;
    self.other=nil;
    self.indexPath=nil;
 
    [self.mTextView resignFirstResponder];
    self.mTextView.delegate=nil;
    [self.mTextView removeobserver];
    [self.mTextView removeFromSuperview];
    self.mTextView=nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
   
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model other:(id)other key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.data =model;
    self.other =other;
    self.indexPath =indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    UIImage *imag =  [UIImage imageNamed:@"cjsd_image"];
    int isImage =[[dict objectForKey:@"image"] intValue];
    CGFloat H =imag.size.height;
    if (isImage) {
        CGFloat w =[[dict objectForKey:@"width"] floatValue];
        CGFloat h =[[dict objectForKey:@"height"] floatValue];
        if (w>0 && h>0) {
            CGFloat fullW =DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4;
            CGFloat ratio = fullW / w;
            H = ratio * h;
        }else{
            H=imag.size.height;
        }
    }
    self.imagesView.frame =CGRectMake(PHOTO_FRAME_WIDTH*2, 0, DEVICE_WIDTH -PHOTO_FRAME_WIDTH*4,  H);
    if (key!=nil && ![key isEqual:[NSNull null]]) {
        self.imagesView.image =key;
    }
    self.mTextView.frame =CGRectMake(PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH+H, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*4);
    NSString *txtstr =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"txt"]] replaceNullString];
    if (txtstr.length==0 || [txtstr isEqualToString:@"(null)"])
    {
        txtstr =@"";
    }
    if (txtstr && txtstr.length>0){
        self.mTextView.textColor = [UIColor darkGrayColor];
        self.mTextView.text =txtstr;
        float height = [[NSString stringWithFormat:@"%@\n ",txtstr] boundingRectWithSize:CGSizeMake(DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:FONT(16),NSFontAttributeName, nil] context:nil].size.height;
        
        self.mTextView.frame =CGRectMake(PHOTO_FRAME_WIDTH*2, PHOTO_FRAME_WIDTH+H, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, height);
    }
   
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary*)model;
    UIImage *imag =  [UIImage imageNamed:@"cjsd_image"];
    CGFloat H =imag.size.height;
    int image =[[dict objectForKey:@"image"] intValue];
    if (image) {
        CGFloat w =[[dict objectForKey:@"width"] floatValue];
        CGFloat h =[[dict objectForKey:@"height"] floatValue];
        if (w>0 && h>0) {
            CGFloat fullW =DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4;
            CGFloat ratio = fullW / w;
            H = ratio * h;
        }else{
            H=imag.size.height;
        }
    }
    NSString *content =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"txt"]] replaceNullString];
    float height = [[NSString stringWithFormat:@"%@\n ",content] boundingRectWithSize:CGSizeMake(DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:FONT(16),NSFontAttributeName, nil] context:nil].size.height;
    H =  H + height;
    return H;
   
}
-(void)handleSingleTapForSunDetail
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_shaidan,self,self.data,self.key,self.indexPath);
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.mTextView resignFirstResponder];
    return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_shaidanPosition,self,self.data,self.mTextView.text,self.indexPath);
    }
    return YES;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

-(void)textDidChangedWillHide:(NSNotification*)notification
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_shaidanTxt,self,self.data,self.mTextView.text,self.indexPath);
    }
}

@end
